<template>
	<view class="work-container">
		<view>
			<uni-notice-bar  @getmore="getMore" showGetMore moreText="查看更多" scrollable showIcon text="这是 NoticeBar 通告栏，这是 NoticeBar 通告栏，这是 NoticeBar 通告栏"></uni-notice-bar>
		</view>
		<uni-section title="待办事项" type="line" title-font-size="45rpx"></uni-section>
		<view class="content">
			<view class="todo-header">
				<!-- 状态栏左侧 -->
				<view class="todo-header_left">
					<text class="active-text">{{ text }}</text>
					<text>{{ listData.length }}条</text>
				</view>
				<!-- 状态栏右侧 -->
				<view class="todo-header_right" v-for="(item, index) in tabList" :key="index">
					<view class="todo-header_right-item" :class="{ 'active-tab': tabIndex == index }" @click="tabClick(item, index)">{{ item }}</view>
				</view>
			</view>
			<view class="todo-content" v-for="(item, index) in listData" :key="item.todoName">
				<view class="todo-list" :class="{ 'todo--finish': item.todoStatus === 0 }" @click="childItem(item, index)">
					<view class="todo-list_checkbox"><view class="checkbox"></view></view>
					<view class="todo-list_content">{{ item.todoName }}</view>
				</view>
			</view>
			<!-- 字体图标 -->
			<view class="create-todo" @click="creat"><text class="iconfont iconhao1" :class="{ 'create-todo-active': textShow }"></text></view>
			<!-- 输入框 -->
			<view class="create-content" v-if="activeInput" :class="{ 'create-show': textShow }">
				<view class="create-content-box">
					<view class="create-input">
						<input type="text" v-model="inputValue" placeholder="请输入你要创建的todo"/>
					</view>
					<view class="creat-button" @click="submitInput">创建</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
  import { getTodo, listTodo, updateTodo, addTodo } from "@/api/project/todo.js"
  
  export default {
    onLoad: function() {
		this.getList();
    },
	data() {
	  return {
		todoList: [],
		inputValue: '',
		inputLimitTime: '',
		activeInput: false,
		tabIndex: 0,
		textShow: false,
		text: '全部',
		tabList: ['全部', '待办', '已完成'],
		list: [],
		form: {},
	  }
	},
	methods: {
	  /** 提交按钮 */
	  submitForm() {
		this.$refs["form"].validate(valid => {
		  if (valid) {
			if (this.form.todoId != null) {
			  updateTodo(this.form).then(response => {
				this.$modal.msgSuccess("修改成功");
				this.open = false;
				this.getList();
			  });
			} else {
			  addTodo(this.form).then(response => {
				this.$modal.msgSuccess("新增成功");
				this.open = false;
				this.getList();
			  });
			}
		  }
		});
	  },
	  getList() {
	  	this.loading = true;
	    listTodo(this.queryParams).then(response => {
			this.todoList = response.rows;
	  	    this.total = response.total;
	  		this.loading = false;
	    })
	  },
	  //切换tab
	  tabClick(item, index) {
	  	this.text = item;
	  	this.tabIndex = index;
	  },
	  //点击项目
	  childItem(item, index) {
		const updatedStatus = this.todoList[index].todoStatus === 0 ? 1 : 0;
		const updatedItem = { ...item, todoStatus: updatedStatus };
		
		updateTodo(updatedItem).then(() => {
			this.$modal.msgSuccess("状态更新成功");
			this.todoList[index].todoStatus = updatedStatus; // 更新本地状态
		}).catch(() => {
			this.$modal.msgError("状态更新失败");
		});
		
	  },
	  //打开输入框
	  creat() {
	  	if (this.activeInput) {
	  		this.close();
	  	} else {
	  		this.open();
	  	}
	  },
	  //打开动画
	  open() {
	  	this.activeInput = true;
	  	this.$nextTick(() => {
	  		setTimeout(() => {
	  			this.textShow = true;
	  		}, 50);
	  	});
	  },
	  //关闭动画
	  close() {
	  	this.textShow = false;
	  	this.$nextTick(() => {
	  		setTimeout(() => {
	  			this.activeInput = false;
	  		}, 350);
	  	});
	  },
	  //点击创建
	  submitInput() {
	  	if (this.inputValue == '' || this.inputLimitTime == '') {
	  		uni.showToast({
	  			title: '输入的内容不能为空',
	  			icon: 'none'
	  		});
	  	} else {
	  		const newTodo = {
				todoName: this.inputValue,
				todoStatus: 0 // 默认新增为待办状态
			};
			
			// 调用后端API新增事项
			addTodo(newTodo).then(() => {
				this.$modal.msgSuccess("待办事项新增成功");
				this.getList(); // 重新获取列表
				this.close(); // 收起输入框
			}).catch(() => {
				this.$modal.msgError("新增失败，请稍后重试");
			});
	  		
	  	}
	  },
	  getMore() {
		uni.showToast({
			title: '点击查看更多',
			icon: 'none'
		})
	  }
	},
	computed: {
		listData() {
			let list = JSON.parse(JSON.stringify(this.todoList)); //拷贝对象
			let newList = [];
			//点击全部
			if (this.tabIndex == 0) {
				return list;
			}
			//点击待办
			if (this.tabIndex == 1) {
				list.forEach(v => {
					if (v.todoStatus == 1) {
						newList.push(v);
					}
				});
				return newList;
			}
			//点击已完成
			if (this.tabIndex == 2) {
				list.forEach(v => {
					if (v.todoStatus == 0) {
						newList.push(v);
					}
				});
				return newList;
			}
		}
	},
  }
</script>

<style>
  .content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
  
  .container {
	  padding: 20px;
  }

  .logo {
    height: 200rpx;
    width: 200rpx;
    margin-top: 200rpx;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 50rpx;
  }

  .text-area {
    display: flex;
    justify-content: center;
  }

  .title {
    font-size: 36rpx;
    color: #8f8f94;
  }
</style>

<style lang="scss">
  /* #ifndef APP-NVUE */
  page {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    background-color: #fff;
    min-height: 100%;
    height: auto;
  }

  view {
    font-size: 20px;
    line-height: inherit;
  }

  /* #endif */

  .text {
    text-align: center;
    font-size: 36rpx;
    margin-top: 10rpx;
  }

  .grid-item-box {
    flex: 1;
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 15px 0;
  }

  .uni-margin-wrap {
    width: 690rpx;
    width: 100%;
  }
  
  .todo-header {
  	display: flex;
  	align-items: center;
  	font-size: 15px;
	width: 100%;
  	color: #333333;
  	height: 45px;
  	box-shadow: -1px 1px 5px 0 rgba(0, 0, 0, 0.1);
  	background-color: #ffffff;
  }
  .todo-header_left {
  	width: 70%;
	font-size: 20px;
  }
  .active-text {
  	font-size: 14px;
  	color: #279abf;
  	padding-right: 10px;
  }
  .todo-header_right {
  	display: flex;
  	flex-shrink: 0;
  }
  .todo-header_right-item {
  	padding: 0 5px;
	font-size: 20px;
  }
  .active-tab {
  	color: #279abf;
  }
  .todo-content {
  	position: relative;
	width: 100%;
  }
  .todo-list {
  	position: relative;
  	display: flex;
  	align-items: center;
	height: 35px;
  	padding: 10px;
  	margin: 10px;
  	color: #0c3854;
  	font-size: 12px;
  	border-radius: 10px;
  	background: #cfebfd;
  	box-shadow: -1px 1px 5px 1px rgba(0, 0, 0, 0.1), -1px 2px 1px 0 rgba(255, 255, 255, 0.1) inset;
  	overflow: hidden;
  }
  .todo-list::after {
  	position: absolute;
  	content: '';
  	top: 0;
  	bottom: 0;
  	left: 0;
  	width: 5px;
  	background: #91d1e8;
  }
  .todo-list_checkbox {
  	padding-right: 15px;
  }
  .checkbox {
  	width: 20px;
  	height: 20px;
  	border-radius: 50%;
  	background: #ffffff;
  	box-shadow: 0 0 5px 1px rgba(0, 0, 0, 0.1);
  }
  .todo--finish .checkbox {
  	position: relative;
  	background: #eee;
  }
  .todo--finish .checkbox::after {
  	content: '';
  	position: absolute;
  	width: 10px;
  	height: 10px;
  	top: 0;
  	left: 0;
  	right: 0;
  	bottom: 0;
  	border-radius: 50%;
  	margin: auto;
  	background: #cfebfd;
  	box-shadow: 0 0 2px 0px rgba(0, 0, 0, 0.2) inset;
  }
  .todo--finish .todo-list_content {
  	color: #999;
	font-size: 18px;
  }
  .todo--finish.todo-list:before {
  	content: '';
  	position: absolute;
  	top: 0;
  	bottom: 0;
  	left: 40px;
  	right: 10px;
  	height: 2px;
  	margin: auto 0;
  	background: #bdcdd8;
  }
  .todo--finish.todo-list:after {
  	background: #ccc;
  }
  .create-todo {
  	display: flex;
  	align-items: center;
  	justify-content: center;
  	position: fixed;
  	bottom: 20px;
  	left: 0;
  	right: 0;
  	margin: 0 auto;
  	width: 50px;
  	height: 50px;
  	border-radius: 50%;
  	background-color: #cfebfd;
  	box-shadow: -1px 1px 5px 2px rgba(0, 0, 0, 0.1), -1px 1px 1px 0 rgba(255, 255, 255, 0.1) inset;
  }
  .iconhao1 {
  	font-size: 30px;
  	color: #a7d2de;
  }
  .create-content {
  	position: fixed;
  	bottom: 95px;
  	left: 20px;
  	right: 20px;
  	transition: all 0.3s;
  	opacity: 0;
  	transform: scale(0) translateY(200%);
  }
  .create-show {
  	opacity: 1;
  	transform: scale(1) translateY(0);
  }
  .create-content-box {
  	display: flex;
  	align-items: center;
  	padding: 0 15px;
  	padding-right: 0;
  	border-radius: 20px;
  	background: #cfebfd;
  	box-shadow: -1px 1px 5px 2px rgba(0, 0, 0, 0.1), -1px 1px 1px 0 rgba(255, 255, 255, 0.1) inset;
  	z-index: 2;
  }
  .create-input {
  	width: 100%;
  	padding-right: 15px;
	font-size: 20px;
  	color: #0c3854;
  }
  .creat-button {
  	display: flex;
  	align-items: center;
  	justify-content: center;
  	flex-shrink: 0;
  	height: 40px;
  	width: 70px;
  	border-radius: 30px;
  	font-size: 16px;
  	color: #88d4ec;
  	box-shadow: -2px 0px 2px 1px rgba(0, 0, 0, 0.1);
  }
  .create-content:after {
  	content: '';
  	position: absolute;
  	right: 0;
  	left: 0;
  	bottom: -8px;
  	width: 20px;
  	height: 20px;
  	background: #cfebfd;
  	transform: rotate(45deg);
  	margin: 0 auto;
  	box-shadow: 1px 1px 5px 2px rgba(0, 0, 0, 0.1);
  	z-index: -1;
  }
  .create-content-box:after {
  	content: '';
  	position: absolute;
  	right: 0;
  	left: 0;
  	bottom: -8px;
  	width: 20px;
  	height: 20px;
  	background: #cfebfd;
  	transform: rotate(45deg);
  	margin: 0 auto;
  }
  .iconhao1 {
  	transition: transform 0.5s;
  }
  .create-todo-active {
  	transform: rotate(135deg);
  }
</style>

